// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <melon/benchmark.h>
#include <melon/init/init.h>

#include <turbo/flags/flag.h>

#include <pollux/common/utils.h>
#include <pollux/common/memory/memory.h>
#include <pollux/vector/vector_fuzzer.h>

TURBO_FLAG(int64_t,fuzzer_seed, 99887766, "Seed for random input dataset generator");

namespace {

using namespace kumo::pollux;

memory::MemoryPool* pool() {
  static auto leaf = memory::MemoryManager::getInstance()->addLeafPool();
  return leaf.get();
}

VectorFuzzer::Options getOpts(size_t n, double nullRatio = 0) {
  VectorFuzzer::Options opts;
  opts.vectorSize = n;
  opts.nullRatio = nullRatio;
  return opts;
}

BENCHMARK_MULTI(flatInteger, n) {
  VectorFuzzer fuzzer(getOpts(n), pool(), FLAGS_fuzzer_seed);
  melon::doNotOptimizeAway(fuzzer.fuzzFlat(BIGINT()));
  return n;
}

BENCHMARK_RELATIVE_MULTI(flatIntegerHalfNull, n) {
  VectorFuzzer fuzzer(getOpts(n, 0.5), pool(), FLAGS_fuzzer_seed);
  melon::doNotOptimizeAway(fuzzer.fuzzFlat(BIGINT()));
  return n;
}

BENCHMARK_RELATIVE_MULTI(flatDouble, n) {
  VectorFuzzer fuzzer(getOpts(n), pool(), FLAGS_fuzzer_seed);
  melon::doNotOptimizeAway(fuzzer.fuzzFlat(DOUBLE()));
  return n;
}

BENCHMARK_RELATIVE_MULTI(flatBool, n) {
  VectorFuzzer fuzzer(getOpts(n), pool(), FLAGS_fuzzer_seed);
  melon::doNotOptimizeAway(fuzzer.fuzzFlat(BOOLEAN()));
  return n;
}

BENCHMARK_RELATIVE_MULTI(flatVarcharAscii, n) {
  auto opts = getOpts(n);
  opts.charEncodings = {fuzzer::UTF8CharList::ASCII};

  VectorFuzzer fuzzer(opts, pool(), FLAGS_fuzzer_seed);
  melon::doNotOptimizeAway(fuzzer.fuzzFlat(VARCHAR()));
  return n;
}

BENCHMARK_RELATIVE_MULTI(flatVarcharUtf8, n) {
  auto opts = getOpts(n);
  opts.charEncodings = {fuzzer::UTF8CharList::EXTENDED_UNICODE};

  VectorFuzzer fuzzer(opts, pool(), FLAGS_fuzzer_seed);
  melon::doNotOptimizeAway(fuzzer.fuzzFlat(VARCHAR()));
  return n;
}

BENCHMARK_DRAW_LINE();

BENCHMARK_RELATIVE_MULTI(constantInteger, n) {
  VectorFuzzer fuzzer(getOpts(n), pool(), FLAGS_fuzzer_seed);
  melon::doNotOptimizeAway(fuzzer.fuzzConstant(BIGINT()));
  return n;
}

BENCHMARK_RELATIVE_MULTI(dictionaryInteger, n) {
  VectorFuzzer fuzzer(getOpts(n), pool(), FLAGS_fuzzer_seed);
  melon::doNotOptimizeAway(fuzzer.fuzzDictionary(fuzzer.fuzzFlat(BIGINT())));
  return n;
}

BENCHMARK_DRAW_LINE();

BENCHMARK_RELATIVE_MULTI(flatArray, n) {
  VectorFuzzer fuzzer(getOpts(n), pool(), FLAGS_fuzzer_seed);
  const size_t elementsSize = n * fuzzer.getOptions().containerLength;
  melon::doNotOptimizeAway(
      fuzzer.fuzzArray(fuzzer.fuzzFlat(BIGINT(), elementsSize), n));
  return n;
}

BENCHMARK_RELATIVE_MULTI(flatMap, n) {
  VectorFuzzer fuzzer(getOpts(n), pool(), FLAGS_fuzzer_seed);
  const size_t elementsSize = n * fuzzer.getOptions().containerLength;
  melon::doNotOptimizeAway(fuzzer.fuzzMap(
      fuzzer.fuzzFlat(BIGINT(), elementsSize),
      fuzzer.fuzzFlat(BIGINT(), elementsSize),
      n));
  return n;
}

BENCHMARK_RELATIVE_MULTI(flatMapArrayNested, n) {
  VectorFuzzer fuzzer(getOpts(n), pool(), FLAGS_fuzzer_seed);
  const size_t elementsSize = n * fuzzer.getOptions().containerLength;

  melon::doNotOptimizeAway(fuzzer.fuzzMap(
      fuzzer.fuzzFlat(BIGINT(), elementsSize),
      fuzzer.fuzzArray(
          fuzzer.fuzzFlat(BIGINT(), elementsSize * 10), elementsSize),
      n));
  return n;
}

} // namespace

int main(int argc, char* argv[]) {
  melon::Init init{&argc, &argv};
  gflags::ParseCommandLineFlags(&argc, &argv, true);
  memory::MemoryManager::initialize({});
  melon::runBenchmarks();
  return 0;
}
